<?php
	/**
	 * @desc This class is responsible for all manipulation
	 * on HTML inputs and input sanitization.
	 */
	class Action extends Sanitizer{
		
		
		/**
		 * 
		 * @desc Check if some field (input name) is submitted to the server.
		 * @param string $field
		 * @return true/false
		 */	
		public static function submitted($field){
			if (isset($_POST[$field]) && !Joob_Utils_Word::isEmpty($_POST[$field])){
				return true;
			}
			return false;
		}
		
        
        
        /**
        * @desc Check if a file is submitted (check by the name of 
        * the input field.
        * @return true/false.
        */
        public static function submittedFile($field){
            if (isset($_FILES[$field]['name'])){
                $name=$_FILES[$field]['name'];
                if (!Joob_Utils_Word::isEmpty($name)) return true;
            }
            return false;
        }
        
        
		
		/**
		 * @desc Getting an input from HTTP post or get
		 * @param string $field
		 * @param constant string $method (it should be 
		 * post, or :post, or get, or :get).
		 * @return false (not data), or string.
		 */	
		public static function input($field, $method='post'){            
			if ($method=='post' || $method==':post'){
				
				if (isset($_POST[$field."-actual"])){
                    if (get_magic_quotes_gpc()==0){
                        return $_POST[$field."-actual"];
                    }else{
                        return stripslashes($_POST[$field."-actual"]);
                    }
				}
				
				if (isset($_POST[$field])){
                    if (get_magic_quotes_gpc()==0){
                        return $_POST[$field];
                    }else{
                        return stripslashes($_POST[$field]);
                    }
				}
				
				return false;
			}else{
				if (isset($_GET[$field])){
					return $_GET[$field];
				}
				return false;
			}
		}
		
		
			
		/**
		 * @desc Getting the input of HTTP POST or GET. After
		 * getting the data, try to sanitize it.
		 * @param string $field The name of the input.
		 * @param constant $method The method name.
		 */
		public static function inputSanitize($field, $method='post'){
			return Action::sanitize(Action::input($field, $method));
		}
		
        
		
        /**
        * @desc Getting the post/get input array, return an array.
        * @return array 
        * @example 
        * 		If post item-1, item-2, etc, then HTML::inputCluster('item') will
        * 		return array($_POST['item-1'],$_POST['item-2']).
        */
        public static function inputCluster($name, $start=1){
            # Max Param Size is 100.
            $a=array();
            for ($i=$start; $i<100; $i++){
                $field=$name."-".$i;
                $input=Action::input($field);
                if ($input===false){
                    return $a;
                }
                if (!Joob_Utils_Word::isEmpty($input)){
                    $a[]=$input;
                }
            }
            return $a;
        }
   
		
        
        /**
        * @desc Get the input from submitted field, so as the input has to 
        * be an int.
        * @return int 
        */
		public static function inputInt($field, $method='post'){
            $input=Action::input($field,$method);
            if (!Validator::isInt($input)) return false;
            return $input;
        }
        
        
        
		/**
		 * 
		 * @desc Getting the input submitted from an editor. This is treated
		 * differently because some editors allow special (dangerous) characters.
		 * @param string $field
		 * @param const string $method post or get
		 */
		public static function inputEditor($field, $method='post'){
			return Action::sanitizeEditor(Action::input($field, $method));
		}
	}
?>
